<?php
class tejia_orderControl extends skymvc{
	
	public function __construct(){
		parent::__construct();
	}
	
	public function onDefault(){
		$where="  siteid=".SITEID;
		$url=APPADMIN."?m=tejia_order";
		$start=get('per_page','i');
		$limit=20;
		$status=get('status','i');
		$where.=" AND status=".$status;
		$url.="&status=".$status;
		$option=array(
			"where"=>$where,
			"order"=>"order_id DESC",
			"start"=>$start,
			"limit"=>$limit
		);
		$rscount=true;
		$data=M("tejia_order")->select($option,$rscount);
		
		if($data){
			foreach($data as $k=>$v){
				$v['address']=M("tejia_order_address")->selectRow(array("where"=>"order_id=".$v['order_id'],"shop_order"=>"id DESC"));
			 
			 	$v['shop']=M("shop")->selectRow("shopid=".$v['shopid']);
				$v['products']=$this->orderproduct($v['order_id']);
				 
				$data[$k]=$v;
			}
		}
		 
		$pagelist=$this->pagelist($rscount,$limit,$url);
		$order_status_list=$this->config_item('order_status_list');
		$order_type_list=$this->config_item('order_type_list');
		$order_ispay=$this->config_item('order_ispay');
		$this->smarty->assign(array(
			"data"=>$data,
			"rscount"=>$rscount,
			"pagelist"=>$pagelist,
			"order_status_list"=>$order_status_list,
			"order_type_list"=>$order_type_list,
			"order_ispay"=>$order_ispay,
			
		));
		$this->smarty->display("tejia_order/index.html");
	}
	
	public function onSHow(){
		$order_id=get('order_id','i');
		$data=M("tejia_order")->selectRow(array("where"=>"order_id=".$order_id));
		if(empty($data)) $this->goall("参数出错",1);
		if($data['siteid']!=SITEID){
			$this->goall("暂无权限",1);
		}
		$shop=M("shop")->selectRow("shopid=".$data['shopid']);
		$addr=M("tejia_order_address")->selectRow(array("where"=>"order_id=".$order_id,"order"=>"id DESC"));
		 
		$shop_order_status_list=$this->config_item('order_status_list');
		
		$shop_order_type_list=$this->config_item('order_type_list');
		$shop_order_ispay=$this->config_item('order_ispay');
		//获取商品
		 
		$shop_order_product=M("tejia_order_product")->select(array("where"=>"order_id=".$data['order_id']));
		$total_num=0;
		foreach($shop_order_product as $k=>$v){
			$p=M("tejia")->selectRow(array("where"=>"id=".$v['object_id']));
			$p['order_price']=$v['price'];
			$p['amount']=$v['amount'];
			$total_num+=$v['amount']; 
			$shop_order_product[$k]=$p;
		}
		 
		$this->smarty->assign(array(
			"data"=>$data,
			"addr"=>$addr,
			"order_status_list"=>$shop_order_status_list,
			"order_type_list"=>$shop_order_type_list,
			"order_ispay"=>$shop_order_ispay,
			"order_product"=>$shop_order_product,
			"shop"=>$shop,
			"total_num"=>$total_num,
		));
		$this->smarty->display("tejia_order/show.html");
	}
	
	public function orderproduct($order_id){
		
		$shop_order_product=M("tejia_order_product")->select(array("where"=>"order_id=".intval($order_id)));
		foreach($shop_order_product as $k=>$v){
			$p=M("tejia")->selectRow(array("where"=>"id=".$v['object_id']));
			$p['shop_order_price']=$v['price'];
			$p['amount']=$v['amount'];
			$shop_order_product[$k]=$p;
		}
		return $shop_order_product;	
	}	
}
?>